#include<bits/stdc++.h>
#define N 200005
using namespace std;
int n;
int a[N];
int f[205][205][205];
map<pair<int,int>,int> F[2];
void sub(){
    for(int i=0;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                f[i][j][k]=-1e9;
    f[0][1][n]=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                if(f[i-1][j][k]<0) continue;
                f[i][max(j,a[i])][k]=max(f[i][max(j,a[i])][k],f[i-1][j][k]+(a[i]>=j));
                f[i][j][min(k,a[i])]=max(f[i][j][min(k,a[i])],f[i-1][j][k]+(a[i]<=k));
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            ans=max(ans,f[n][i][j]);
    printf("%d",ans);
}
void Max(int &x,int y){(x<y)&&(x=y);}
signed main(){
    freopen("shung.in","r",stdin);
    freopen("shung.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    if(n<=200){sub();return 0;}
    F[0][{0,n+1}]=0;
    int o=0;
    for(int i=1;i<=n;i++){
        o^=1;
        F[o].clear();
        for(auto j:F[o^1]){
            int u=j.first.first,v=j.first.second,w=j.second;
            if(!F[o].count({max(u,a[i]),v})) F[o][{max(u,a[i]),v}]=w+(u<a[i]);
            else Max(F[o][{max(u,a[i]),v}],w+(u<a[i]));
            if(!F[o].count({u,min(v,a[i])})) F[o][{u,min(v,a[i])}]=w+(v>a[i]);
            else Max(F[o][{u,min(v,a[i])}],w+(v>a[i]));
        }
    }
    int ans=0;
    for(auto i:F[o]) ans=max(ans,i.second);
    printf("%d",ans);
    return 0;
}